#include <iostream>
#include <fstream>
#include "contact.pb.h"
using namespace google::protobuf;
using namespace std;
// using namespace c_contacts;

void PrintfContacts(const c_contacts::Contacts &contacts)
{
    cout << ".......打印联系人........" << endl;
    for (int i = 0; i < contacts.contacts_size(); i++) // 大小
    {
        const c_contacts::PeopleInfo &people = contacts.contacts(i);
        cout << "联系人姓名: " << i + 1;
        cout << people.name() << endl;
        cout << "年龄: " << i + 1;
        cout << people.age() << endl;
        int j = 1;
        for (const auto &phone : people.phone()) // 大小
        {
            cout << "联系⼈电话" << j++ << ": " << phone.number() << endl;
        }
        const Reflection *reflection = c_contacts::PeopleInfo::GetReflection();
        const UnknownFieldSet &set = reflection->GetUnknownFields(people);
        for (int j = 0; j < set.field_count(); j++)
        {
            const UnknownField &unknown_field = set.field(j);
            cout << "未知字段" << j + 1 << ":  "
                 << "  编号：" << unknown_field.number();
            switch (unknown_field.type())
            {
            case UnknownField::Type::TYPE_VARINT:
                cout << "  值：" << unknown_field.varint() << endl;
                break;
            case UnknownField::Type::TYPE_LENGTH_DELIMITED:
                cout << "  值：" << unknown_field.length_delimited() << endl;
                break;
            default:
                cout<<"错误"<<endl;
                break;
            }
        }
    }
}
int main()
{
    c_contacts::Contacts _con;
    // 先读取已存在的 contacts
    fstream input("../text.bin", ios::in | ios::binary);
    if (!_con.ParseFromIstream(&input))
    {
        cerr << "Failed to parse contacts." << endl;
        input.close();
        return -1;
    }
    // 打印 contacts
    PrintfContacts(_con);
    input.close();
    return 0;
}
